﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Library
{
    class Program
    {
        static string Author; 
        static int Price;
        static int Age;
        static string Brend;
        static int Size;
        static string Name;
        static string DateOfRelease;
        static bool bonus;
        static string Bonus;
        static string TypeCover;
        static bool success = false;

        static void StreamWriter(List<Makulatura> Mak)
        {
            FileInfo file = new FileInfo("name.txt");
            file.Create();
            StreamWriter sw = new StreamWriter("new.txt");
            foreach (Makulatura m in Mak)
                Name = m.ToString();

            sw.WriteLine(Name);
            sw.Close();   
        }

        static void Sorting(List<Makulatura> Mak)
        {
            Console.WriteLine("unordered: ");
            foreach (Makulatura m in Mak)
                Console.WriteLine(m.name + " size " + m.size);
            Mak.Sort();
            Console.WriteLine("ordered: ");
            foreach (Makulatura m in Mak)
                Console.WriteLine(m.name + " size " + m.size);
        }
        static void AddMz(List<Makulatura> Mak) // todo лучше все таки List<Mak> как член класс, а не передавать его по ссылке в каждый метод
        {
            while (success != true)
            {
                Console.WriteLine("Input Price: ");
                string price = Console.ReadLine();
                if (int.TryParse(price, out Price))
                    success = true;
                else
                    Console.WriteLine(" Bad price, try once more ");
            }
            success = false;
            while (success != true)
            {
                Console.WriteLine("Input Size: ");
                string size = Console.ReadLine();
                if (int.TryParse(size, out Size))
                    success = true;
                else
                    Console.WriteLine(" Bad size, try once more ");
            }
            success = false;
            Console.WriteLine("Input Brend: ");
            Brend = Console.ReadLine();
            while (success != true)
            {
                Console.WriteLine("Input bonus (y/n): ");
                Bonus = Console.ReadLine();
                Bonus = Bonus.ToUpper();
                switch (Bonus)
                {
                    case "Y": bonus = true; success = true; break;
                    case "N": bonus = false; success = true; break;
                }
            }
            Console.WriteLine("Input Date of release: ");
            DateOfRelease = Console.ReadLine(); // todo проверка на блондинку где?
            Magazine Mz = new Magazine(bonus, DateOfRelease, Age, Price, Brend, Size);
            Console.WriteLine("Name: ");
            Console.WriteLine(Mz.name);
            Mak.Add(Mz);
        }
        static void AddBook(List<Makulatura> Mak)
        {
            Console.WriteLine("Input Author: ");
            Author = Console.ReadLine();
            Console.WriteLine("Input Age: ");
            string age = Console.ReadLine();
            Age = int.Parse(age); // todo проверка на блондинку где?
            Console.WriteLine("Input price: ");
            string price = Console.ReadLine();
            Price = int.Parse(price); // todo проверка на блондинку где?
            Console.WriteLine("Input brend: ");
            Brend = Console.ReadLine();
            Console.WriteLine("Input size: ");
            string size = Console.ReadLine(); 
            Size = int.Parse(size);
            Console.WriteLine("Input name: ");
            Name = Console.ReadLine();
            Book book = new Book(Author, Age, Price, Brend, Size, Name);
            Mak.Add(book);
        }
        static void AddNewspaper(List<Makulatura> Mak)
        {
            Console.WriteLine("Input Date of release: ");
            DateOfRelease = Console.ReadLine(); // todo проверка на блондинку где?
            Console.WriteLine("Input Price: ");
            string price = Console.ReadLine();
            Price = int.Parse(price); // todo проверка на блондинку где?
            Console.WriteLine("Input Brend: ");
            Brend = Console.ReadLine();
            Console.WriteLine("Input size: ");
            string size = Console.ReadLine();
            Size = int.Parse(size);
            Newspaper newspaper = new Newspaper(DateOfRelease, Price, Brend, Size);
            Mak.Add(newspaper);
        }
        static void AddBooklet(List<Makulatura> Mak)
        {
            Console.WriteLine("Input Price: ");
            string price = Console.ReadLine();
            Price = int.Parse(price); // todo проверка на блондинку где?
            Console.WriteLine("Input Brend: ");
            Brend = Console.ReadLine();
            Console.WriteLine("Input size: ");
            string size = Console.ReadLine();
            Size = int.Parse(size); // todo проверка на блондинку где?
            Console.WriteLine("Input Author: ");
            Author = Console.ReadLine();
            Console.WriteLine("Input Type of cover: ");
            TypeCover = Console.ReadLine();
            Booklet booklet = new Booklet(TypeCover, Author, Price, Brend, Size);
            Mak.Add(booklet);
        }
        static void AllList(List<Makulatura> Mak)
        {
            for (int i = 0; i < Mak.Count; i++)
            {
                Console.WriteLine(Mak[i].name);
                Console.WriteLine(Mak[i].price);
                Console.WriteLine(Mak[i].size);
            }
        }
        static void NotAllList(List<Makulatura> Mak, string nameOfClass) 
        {
            nameOfClass = nameOfClass.ToUpper();
            foreach (object obj in Mak)
            {
                switch (nameOfClass)
                {
                    case "BOOK": 
                        if (obj is Book)
                            Console.WriteLine(((Book)obj).name); break;
                    case "MAGAZINE":
                        if (obj is Magazine)
                            Console.WriteLine(((Magazine)obj).name); break;
                    case "BOOKLET":
                        if (obj is Booklet)
                            Console.WriteLine(((Booklet)obj).name); break;
                    case "NEWSPAPER":
                        if (obj is Newspaper)
                            Console.WriteLine(((Newspaper)obj).name); break;
                    default: throw new MethodNotAllListException("Not such class");
                }
            } 
        }
        static void Main(string[] args)
        {
            try
            {
                string choice, choice1 = "y";
                List<Makulatura> Mak = new List<Makulatura>();
                while (choice1 == "y")
                {
                    Console.WriteLine("Do you want to do smth? (y/n)");
                    choice1 = Console.ReadLine();
                    if (choice1 == "y")
                    {
                        Console.WriteLine("What do you want to do: \r\n");
                        Console.WriteLine("(1) Add magazine \r\n(2) Add Book \r\n(3) Add Newspaper \r\n(4) Add Booklet  \r\n(5) All Elements \r\n(6) Not All Elements \r\n(7)Sorting \r\n(8)To txt \r\n(9)Exit");
                        choice = Console.ReadLine();
                        switch (choice)
                        {
                            case "1": AddMz(Mak); break;
                            case "2": AddBook(Mak); break;
                            case "3": AddNewspaper(Mak); break;
                            case "4": AddBooklet(Mak); break;
                            case "5": AllList(Mak); break;
                            case "6": Console.WriteLine("What class do you want?\r\n(1) Magazine \r\n(2) Book \r\n(3) Newspaper \r\n(4) Booklet  \r\n"); choice1 = Console.ReadLine();
                                switch (choice1)
                                {
                                    case "1": NotAllList(Mak, "Magazine"); break;
                                    case "2": NotAllList(Mak, "Book"); break;
                                    case "3": NotAllList(Mak, "Newspaper"); break;
                                    case "4": NotAllList(Mak, "Booklet"); break;
                                        //проверку надо запилить                                
                                }
                                break;
                            case "7": Sorting(Mak); break;
                            case "8": StreamWriter(Mak); break;
                            case "9": break;
                            default: throw new AddException("Fail in add");
                        }
                    }
                }
            }
            catch (AddException)
            {
                Console.WriteLine("Fail in choosing add methods");
            }
            catch (MethodNotAllListException)
            {
                Console.WriteLine("Fail in chossing class");
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}
